*****
* Market Exposure Table (Table 2)
*****
use "${data}/URB_indiv_wide.dta", clear
keep if consent_endline==1

* Horse race between home, census market, pre-COID market, and post-COVID market
gen deltm = earn_month6 - earn_month5
local rate rate_0601

* Merge in covid rates by district
foreach dist in home cens endl preC post {
	rename district_`dist' district_code
	merge m:1 district_code using "${data}/BGD_district_COVID_cases.dta", ///
		nogen keepusing(`rate') keep(match master)
	rename `rate' rate_`dist'
	rename district_code district_`dist'
}

* Make regression table
label var rate_post "\multirow{2}{*}{\shortstack[l]{Residence \\ (July 2020) }}"
label var rate_preC "\multirow{2}{*}{\shortstack[l]{Labor Market \\ (March 2020) }}"
label var rate_endl "\multirow{2}{*}{\shortstack[l]{Labor Market \\ (April 2019) }}"
label var rate_cens "\multirow{2}{*}{\shortstack[l]{Labor Market \\ (Sept 2018) }}"
label var rate_home "\multirow{2}{*}{\shortstack[l]{Residence \\ (Native) }}"

eststo clear
eststo: reg deltm rate_post, r
eststo: reg deltm rate_post rate_preC, r
eststo: reg deltm rate_post rate_endl, r
eststo: reg deltm rate_post rate_cens, r
eststo: reg deltm rate_post rate_home, r
eststo: reg deltm rate_post rate_preC rate_endl rate_cens rate_home, r


local col1 " & \multicolumn{6}{c}{Change in Household Earnings from May 2019 to May 2020} \\ "
local col2 " & (1) & (2) & (3) & (4) & (5) & (6) \\ \midrule"
local col3 " \multicolumn{7}{l}{COVID-19 rate as of June 1 2020 in worker's district of:} \\"
esttab using "${tables}/Table_2.tex", replace se tex label  mlabels(none) ///
	nonotes nonumbers nostar b(3) starlevels(* 0.1 ** 0.05 *** 0.01) ///
	stats(r2 N, fmt(2 0) labels("R-Squared" "Observations")) ///
	posthead(`col1' `col2' `col3')
	
*****
* Event Study Graphs (Figures 5B and 6C)
*****
use "$data/URB_indiv_long.dta", clear

* Coefficients and SEs
areg earn_month ib1.round##migr, a(market_id) vce(cluster uidno)
predict beta, xb
predict stde, stdp

areg earn_month ib1.round##migr#high_mig, a(market_id) vce(cluster uidno)
predict beta2, xb
predict stde2, stdp

collapse beta stde beta2 stde2, by(migr round high_mig) fast
drop if migr==.

gen se_hi = beta + 1.96*stde
gen se_lo = beta - 1.96*stde
gen se2_hi = beta2 + 1.96*stde2
gen se2_lo = beta2 - 1.96*stde2

* Spread out for the time gaps
recode round (5=5.5) (6=7.5)

* Figure 5B
graph set window fontface "CMU Serif"
twoway (rarea se_lo se_hi round if mig==0 & round<5, color(gs0%15)  lwidth(none)) ///
		(rarea se_lo se_hi round if mig==1 & round<5, color(gs0%15)  lwidth(none)) ///
		(connected beta round if mig==0 & round<5, msymbol(O) color(gs0)) ///
		(connected beta round if mig==1 & round<5, msymbol(T) color(red*.8)) ///
		, xlabel(1  "Oct 2018" 2 "Nov 2018" 3 "Dec 2018" 4 "Jan 2019", angle(45)) ///
		xlabel(4.5 "", add custom tstyle(minor_nolabel)) ///
		xlabel(4.25 "May 2019", angle(45) add custom labcolor(gs0%0) tlcolor(gs0%0)) /// add an invisible tick to make uniform label widths!
		ylabel(0(150)450) xtitle("") ytitle("Monthly Earnings (USD PPP)") ///
		legend(on order(4 "Migrant Workers" 3 "Local Workers")) ///
		graphregion(color(white)) plotregion(margin(5 0 0 0)) name(g1, replace)

twoway (rcap se_lo se_hi round if mig==0 & round==5.5, lcolor(gs0) msize(small) lwidth(thin)) ///
		(rcap se_lo se_hi round if mig==1 & round==5.5, lcolor(red*.8) msize(small) lwidth(thin)) ///
		(scatter beta round if mig==0 & round==5.5, msymbol(O) mcolor(gs0)) ///
		(scatter beta round if mig==1 & round==5.5, msymbol(T) mcolor(red*.8)) ///
		, xlabel(5.5 "May 2019", angle(45)) legend(off) fxsize(25) ///
		xlabel(5.25 "" 5.75 "", add custom tstyle(minor_nolabel)) ///
		ylabel(0(150)450) xtitle("") ytitle("") yscale(off) ///
		graphregion(color(white)) plotregion(margin(0 0 0 0)) name(g2, replace)
		
local cdate 6.75
twoway (rcap se_lo se_hi round if mig==0 & round==7.5, lcolor(gs0) msize(small) lwidth(thin)) ///
		(rcap se_lo se_hi round if mig==1 & round==7.5, lcolor(red*.8) msize(small) lwidth(thin)) ///
		(scatter beta round if mig==0 & round==7.5, msymbol(O) mcolor(gs0)) ///
		(scatter beta round if mig==1 & round==7.5, msymbol(T) mcolor(red*.8)) ///
		(scatteri 0 `cdate' 431 `cdate', recast(line) lcolor(gs0) lpattern(shortdash)) ///
		, xlabel(7.5 "May 2020", angle(45)) text(441 `cdate' "COVID-19")  legend(off) fxsize(35) ///
		xlabel(6.5 "", add custom tstyle(minor_nolabel)) ///
		ylabel(0(150)450) yscale(off) xtitle("") ytitle("") ///
		graphregion(color(white)) plotregion(margin(0 5 0 0)) name(g3, replace)
		
		
grc1leg g1 g2 g3, graphregion(color(white)) cols(3) imargin(small) legendfrom(g1)
graph export "$figures/Figure_5B.png", replace
graph drop _all

** Figure 6C
replace round = round-.02 if round>5 & mig==0
replace round = round+.05 if round>5 & high_mig==1

twoway (rarea se2_lo se2_hi round if mig==0 & high_mig==0 & round<5, color(gs0%15)  lwidth(none)) ///
		(rarea se2_lo se2_hi round if mig==1 & high_mig==0 & round<5, color(gs0%15)  lwidth(none)) ///
		(rarea se2_lo se2_hi round if mig==0 & high_mig==1 & round<5, color(gs0%15)  lwidth(none)) ///
		(rarea se2_lo se2_hi round if mig==1 & high_mig==1 & round<5, color(gs0%15)  lwidth(none)) ///
		(connected beta2 round if mig==0 & high_mig==0 & round<5, msymbol(O) color(gs0)) ///
		(connected beta2 round if mig==1 & high_mig==0 & round<5, msymbol(T) color(red*.8)) ///
		(connected beta2 round if mig==0 & high_mig==1 & round<5, msymbol(Oh) color(gs0) lpattern(dash)) ///
		(connected beta2 round if mig==1 & high_mig==1 & round<5, msymbol(Th) color(red*.8) lpattern(dash)) ///
		, xlabel(1  "Oct 2018" 2 "Nov 2018" 3 "Dec 2018" 4 "Jan 2019", angle(45)) ///
		xlabel(4.5 "", add custom tstyle(minor_nolabel)) ///
		xlabel(4.25 "May 2019", angle(45) add custom labcolor(gs0%0) tlcolor(gs0%0)) /// add an invisible tick to make uniform label widths!
		ylabel(0(100)500) xtitle("") ytitle("Monthly Earnings (USD PPP)") yscale(range(550)) ///
		legend(on order(6 "Migrants, Low Market" 5 "Locals, Low Market" 8 "Migrants, High Market" 7 "Locals, High Market")) ///
		graphregion(color(white)) plotregion(margin(5 0 0 0)) name(g1, replace)

twoway (rcap se2_lo se2_hi round if mig==0 & high_mig==0 & round>5&round<6, lcolor(gs0) msize(small) lwidth(thin)) ///
		(rcap se2_lo se2_hi round if mig==1 & high_mig==0 & round>5&round<6, lcolor(red*.8) msize(small) lwidth(thin)) ///
		(rcap se2_lo se2_hi round if mig==0 & high_mig==1 & round>5&round<6, lcolor(gs0) msize(small) lwidth(thin)) ///
		(rcap se2_lo se2_hi round if mig==1 & high_mig==1 & round>5&round<6, lcolor(red*.8) msize(small) lwidth(thin)) ///
		(scatter beta2 round if mig==0 & high_mig==0 & round>5&round<6, msymbol(O) mcolor(gs0)) ///
		(scatter beta2 round if mig==1 & high_mig==0 & round>5&round<6, msymbol(T) mcolor(red*.8)) ///
		(scatter beta2 round if mig==0 & high_mig==1 & round>5&round<6, msymbol(Oh) mcolor(gs0)) ///
		(scatter beta2 round if mig==1 & high_mig==1 & round>5&round<6, msymbol(Th) mcolor(red*.8)) ///
		, xlabel(5.5 "May 2019", angle(45)) legend(off) fxsize(25) ///
		xlabel(5.25 "" 5.75 "", add custom tstyle(minor_nolabel)) ///
		ylabel(0(100)500) xtitle("") ytitle("") yscale(off range(550)) ///
		graphregion(color(white)) plotregion(margin(0 0 0 0)) name(g2, replace)
		
local cdate 6.75
twoway (rcap se2_lo se2_hi round if mig==0 & high_mig==0 & round>7, lcolor(gs0) msize(small) lwidth(thin)) ///
		(rcap se2_lo se2_hi round if mig==1 & high_mig==0 & round>7, lcolor(red*.8) msize(small) lwidth(thin)) ///
		(rcap se2_lo se2_hi round if mig==0 & high_mig==1 & round>7, lcolor(gs0) msize(small) lwidth(thin)) ///
		(rcap se2_lo se2_hi round if mig==1 & high_mig==1 & round>7, lcolor(red*.8) msize(small) lwidth(thin)) ///
		(scatter beta2 round if mig==0 & high_mig==0 & round>7, msymbol(O) mcolor(gs0)) ///
		(scatter beta2 round if mig==1 & high_mig==0 & round>7, msymbol(T) mcolor(red*.8)) ///
		(scatter beta2 round if mig==0 & high_mig==1 & round>7, msymbol(Oh) mcolor(gs0)) ///
		(scatter beta2 round if mig==1 & high_mig==1 & round>7, msymbol(Th) mcolor(red*.8)) ///
		(scatteri 0 `cdate' 525 `cdate', recast(line) lcolor(gs0) lpattern(shortdash)) ///
		, xlabel(7.5 "May 2020", angle(45)) text(537 `cdate' "COVID-19")  legend(off) fxsize(35) ///
		xlabel(6.5 "", add custom tstyle(minor_nolabel)) ///
		ylabel(0(100)500) yscale(off range(550)) xtitle("") ytitle("") ///
		graphregion(color(white)) plotregion(margin(0 5 0 0)) name(g3, replace)
		
grc1leg g1 g2 g3, graphregion(color(white)) cols(3) imargin(small) legendfrom(g1)
graph export "$figures/Figure_6C.png", replace
graph drop _all
